#include "Gui.h"

Gui* Gui::instance = NULL;
Gui::Gui(void)
{

}
Gui::~Gui(void)
{
}

Gui* Gui::getInstance()
{
	if( instance!=NULL)
	{
		return instance;

	}
	else
	{
		instance = new Gui();
		return instance;
	}
}

void Gui::handleInput(SDL_Event event)
{
	int x =0;
	int y = 0;
	//find active element if there is one, and call it's handle input method
	std::vector<GuiElement*>::iterator it ;
				for (it = elements.begin(); it!=elements.end();it++)
				{
					GuiElement* e = (*it);
					if(e->isActive)
					{
						e->handleInput(event);
						
					}

				}
	 if( event.type == SDL_MOUSEBUTTONDOWN )
		{
			//If the left mouse button was pressed
			if( event.button.button == SDL_BUTTON_LEFT )
			{
				//disable currently active guielement
			//get element at the coordinates and call its onclick
				//Get the mouse offsets
				x = event.button.x;
				y = event.button.y;
				printf("%i" ,elements.size());
				std::vector<GuiElement*>::iterator it ;
				for (it = elements.begin(); it!=elements.end();it++)
				{
					GuiElement* e = (*it); 
					if(e->isInBounds(x,y))
					{
						e->onClick(); 
					}
					else
					{
						
						e->onClickOut();
					}

				}
			}
		}
		

	}

void Gui::drawElements(SDL_Surface* screen)
{
	std::vector<GuiElement*>::iterator it ;
				for (it = elements.begin(); it!=elements.end();it++)
				{
					GuiElement* e = (*it);
					e->drawElement(screen);

				}

}

void Gui::addElement(GuiElement* element)
{
	elements.push_back(element);
	printf("%i" , elements.size());
}

//this class should contain convenience constructors for every gui element type.
//this way you can make one call, instead of constructing and adding in a seperate call.